<template>
  <!-- 操作栏 -->
  <el-row class="mb-10px" justify="end">
    <el-button @click="openForm('create')">
      <Icon class="mr-5px" icon="ep:edit" />
      创建开票信息
    </el-button>
  </el-row>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="发票抬头" align="center" prop="title" />
      <el-table-column label="纳税人识别号" align="center" prop="taxCode" />
      <el-table-column label="抬头类型" align="center" prop="titleType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CRM_RECEIPT_TITLE" :value="scope.row.titleType" />
        </template>
      </el-table-column>
      <el-table-column label="开户行" align="center" prop="bankName" />
      <el-table-column label="开户行账号" align="center" prop="bankAccount" />
      <el-table-column label="开票地址" align="center" prop="receiptAddress" />
      <el-table-column label="电话号码" align="center" prop="phoneNumber" />
      <el-table-column label="操作" align="center" min-width="120px">
        <!-- <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['crm:receipt-title:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['crm:receipt-title:delete']"
          >
            删除
          </el-button>
        </template> -->
        <template #default="scope">
          <el-button link type="primary" @click="openForm('detail', scope.row.id)">
            查看
          </el-button>
          <el-button link type="primary" @click="openForm('update', scope.row.id)">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ReceiptTitleForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ReceiptTitleApi, ReceiptTitle } from '@/api/crm/receipttitle'
import ReceiptTitleForm from './ReceiptTitleForm.vue'

/** crm发票抬头 列表 */
defineOptions({ name: 'ReceiptTitle' })

const props = defineProps<{
  customerId: number
}>()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ReceiptTitle[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  titleType: undefined,
  title: undefined,
  taxCode: undefined,
  bankName: undefined,
  bankAccount: undefined,
  receiptAddress: undefined,
  phoneNumber: undefined,
  createTime: [],
  ownerUserId: undefined,
  customerId: props.customerId
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ReceiptTitleApi.getReceiptTitlePage(queryParams)
    list.value = data.list
    total.value = data.total
    console.log(list.value)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id, props.customerId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ReceiptTitleApi.deleteReceiptTitle(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除crm发票抬头 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await ReceiptTitleApi.deleteReceiptTitleList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: ReceiptTitle[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ReceiptTitleApi.exportReceiptTitle(queryParams)
    download.excel(data, 'crm发票抬头.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
